package com.it.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.blog.domain.ArticleType;
import com.it.blog.mapper.ArticleTypeMapper;
import com.it.blog.query.ArticleTypeQuery;
import com.it.blog.service.ArticleTypeService;
import com.it.blog.vo.ArticleTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ArticleTypeServiceImpl implements ArticleTypeService {
    @Autowired
    private ArticleTypeMapper articleTypeMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 查询全部
     *
     * @return
     */
    @Override
    public List<ArticleType> findAll() {
        return articleTypeMapper.findAll();
    }

    @Override
    public PageInfo<ArticleType> queryPage(ArticleTypeQuery articleTypeQuery) {
        PageHelper.startPage(articleTypeQuery.getCurrentPage(), articleTypeQuery.getPageSize());
        List<ArticleType> articleTypes = articleTypeMapper.queryPage(articleTypeQuery);
        System.out.println(articleTypes);
        PageInfo<ArticleType> pageInfo = new PageInfo<>(articleTypes);
        return pageInfo;
    }

    @Override
    public ArticleType findOne(Long id) {
        ArticleType articleType = articleTypeMapper.findOne(id);
        return articleType;
    }

    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")//''要加,这是spring的spel表达式的要求!
    public void addArticleType(ArticleType articleType) {
        articleTypeMapper.addArticleType(articleType);
    }

    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")//''要加,这是spring的spel表达式的要求!
    public void updateArticleType(ArticleType articleType) {
        articleType.setUpdateTime(new Date());
        articleTypeMapper.updateArticleType(articleType);
    }

    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")//''要加,这是spring的spel表达式的要求!
    public void deleteArticleType(Long id) {
        articleTypeMapper.deleteArticleType(id);
    }

    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")//''要加,这是spring的spel表达式的要求!
    public void batchArticleTypes(Long[] ids) {
        articleTypeMapper.batchArticleTypes(ids);
    }

    @Override
    @Cacheable(cacheNames = "articleType", key = "'typeTree'")//''要加,这是spring的spel表达式的要求!
    public List<ArticleType> getTypeTree(Long id) {
//        List<ArticleType> articleTypeTreeList=null;
//        Object typeTreeObj = redisTemplate.boundValueOps("typeTree").get();
//        if (typeTreeObj == null) {
//            List<ArticleType> articleTypeTree = getArticleTypeTree(id);
//            redisTemplate.boundValueOps("typeTree").set(articleTypeTree);
//        } else {
//            articleTypeTreeList= (List<ArticleType>) typeTreeObj;
//        }
        return this.getArticleTypeTree(id);
    }

    private List<ArticleType> getArticleTypeTree(Long id) {
        List<ArticleType> articleTypeList = articleTypeMapper.findAll();
        Map<Long, ArticleType> map = new HashMap<>();
        for (ArticleType articleType : articleTypeList) {
            Long Id = articleType.getId();
            map.put(Id, articleType);
        }
        List<ArticleType> treeList = new ArrayList<>();
        for (ArticleType articleType : articleTypeList) {
            //编辑时，不能选自己为父类型
            if (id.equals(articleType.getId())) {
                continue;
            }
            Long parentId = articleType.getParentId();
            if (parentId == null) {
                treeList.add(articleType);
            } else {
                ArticleType articleTypeParent = map.get(parentId);
                articleTypeParent.getChildren().add(articleType);
            }
        }
        return treeList;
    }

    @Override
    public List<ArticleTypeVo> getArticleTypeStatistics() {
        List<ArticleTypeVo> articleTypeVoList = articleTypeMapper.getArticleTypeStatistics();
        return articleTypeVoList;
    }
}
